﻿/*
Audica Uploader - application for convenient uploading to various file hostings
Copyright (C) 2011-2012 Makarov Andrey (Winand)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "singleapplication.h"
#include "networkuploadmanager.h"
#include "global.h"
#define UID "audica_uploader_runflag"
#include <QStyleFactory>
#include "uploadsummary.h"

void parseAppwideOptions(QList<QString>* args) {
    QString opts;
    if(args->length() > 3) { //app *-o ptions* otherstuff
        int optpos = args->indexOf("-o");
        if(optpos != -1 || optpos < args->length() - 1) {
            opts = args->takeAt(optpos + 1).toLower();
            args->removeAt(optpos); //-o
        }
    }
    qApp->setProperty("g_verbose", QVariant(opts.contains('v')));
}

int main(int argc, char *argv[]) {
    SingleApplication a(argc, argv, UID);

    /*UploadSummary sum;
    sum.show();
    return a.exec();*/

#ifdef QT_NO_DEBUG
    qInstallMsgHandler(customDbgHandler);
#endif
    //no need? we don't have to generate boundaries anymore
    /*qsrand(QTime::currentTime().msec()); //init rnd num generator*/
    QApplication::setStyle(QStyleFactory::create("Cleanlooks"));
    //TODO: depends on stay-in-memory option
    qApp->setQuitOnLastWindowClosed(false); //don't exit if there're no windows on screen

    QList<QString> args = a.arguments();
    parseAppwideOptions(&args);
    if(args.length() > 1) {
        args.removeFirst(); //remove program name
        if(!a.isRunning()) { //program start
            NetworkUploadManager manager;
            QObject::connect(&a, SIGNAL(messageAvailable(QList<QString>)),
                             &manager, SLOT(newUploadTaskInput(QList<QString>)));
            manager.newUploadTaskInput(args);
            return a.exec();
        } else a.sendMessage(args);
    }
    return 0; //nothing to upload or program is running
}
